package com.jiang.mysportsnews.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.jiang.mysportsnews.R;
import com.jiang.mysportsnews.entities.DaoMaster;
import com.jiang.mysportsnews.entities.DaoSession;
import com.jiang.mysportsnews.entities.UserLogin;
import com.jiang.mysportsnews.entities.UserLoginDao;

import java.util.List;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText userName;
    private EditText userPsd;
    private DaoSession session;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        init();
    }

    private void init(){
        String string = "没有账号?注册一个";
        int start = string.indexOf("注册");
        int end = start+"注册".length();
        SpannableStringBuilder builder = new SpannableStringBuilder(string);
        builder.setSpan(new ForegroundColorSpan(Color.BLUE),start,end, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
        ((TextView) findViewById(R.id.register_tv)).setText(builder);

        userName = ((EditText) findViewById(R.id.user_username_et));
        userPsd = ((EditText) findViewById(R.id.user_psd_et));
        findViewById(R.id.login_bt).setOnClickListener(this);
        findViewById(R.id.register_tv).setOnClickListener(this);
        findViewById(R.id.remember_psd_tv).setOnClickListener(this);
        findViewById(R.id.back).setOnClickListener(this);

        session = DaoMaster.newDevSession(this,"mysportsnews_db");

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.login_bt:
                if (userName==null || userPsd==null){
                    Toast.makeText(this, "用户名或密码不能为空", Toast.LENGTH_SHORT).show();
                }else {
                    List<UserLogin> list = session.getUserLoginDao().queryBuilder().where(UserLoginDao.Properties.UserName.eq(userName)).list();
                    if (list.size()==0){
                        Toast.makeText(this, "没有此用户", Toast.LENGTH_SHORT).show();
                    }else {
                        for (int i = 0; i < list.size(); i++) {
                            if (list.get(i).getPassword().equals(userPsd)) {
                                Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show();
                            }else {
                                Toast.makeText(this, "密码错误", Toast.LENGTH_SHORT).show();
                            }
                        }
                    }
                }
                break;
            case R.id.register_tv:
                break;
            case R.id.remember_psd_tv:
                break;
            case R.id.back:
                finish();
                break;
        }
    }
}
